package com.auth.client.config.context;

import com.auth.client.config.exception.OpsClientException;
import com.auth0.jwt.algorithms.Algorithm;
import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;

/**
 * @author: mhSui 2020/07/01
 */
@Data
public class AppContext {

    private static final Logger log = LoggerFactory.getLogger(AppContext.class);
    private static AppContext context;
    private Algorithm algorithm;
    private Long appId;
    private Long applicationId;
    private Long productId;
    private String areaId;
    public static final String DEFAULT_SECRET = "adfa1479akpqo0Il1Xm";
    private static final AppContext DEFAULT;

    public AppContext() {
    }

    public static AppContext get() {
        return null == context ? DEFAULT : context;
    }

    public static void set(AppContext context) {
        context = context;
    }

    public Algorithm getAlgorithm() {
        if (null == this.algorithm) {
            throw new OpsClientException("尚未获取到加密密钥.");
        } else {
            return this.algorithm;
        }
    }

    static {
        AppContext context = new AppContext();

        try {
            context.setAlgorithm(Algorithm.HMAC256("adfa1479akpqo0Il1Xm"));
        } catch (UnsupportedEncodingException var2) {
            log.error(var2.getMessage(), var2);
        }
        DEFAULT = context;
    }
}
